#include "QtThreadGraphics.h"
#include "Object.h"

#include <QFile>
#include <QDataStream>

void QtThreadGraphics::paintAll() {
    emit paintSignal( zoomX, zoomY, zoomCenterX,
                      zoomCenterY, getZoomToPlayer(), widget->getField() );
    emit drawAll( widget, halluMode );
}

void QtThreadGraphics::zoomIn( int i ) {
    zoomInX( i );
    zoomInY( i );
}

void QtThreadGraphics::zoomOut( int i ) {
    zoomOutX( i );
    zoomOutY( i );
}

void QtThreadGraphics::zoomInX( int i ) {
    zoomX -= i;
    if( zoomX < MINIMUMSCALEFACTOR ) {
        zoomX = MINIMUMSCALEFACTOR;
    }
}

void QtThreadGraphics::zoomInY( int i ) {
    zoomY -= i;
    if( zoomY < MINIMUMSCALEFACTOR ) {
        zoomY = MINIMUMSCALEFACTOR;
    }
}

void QtThreadGraphics::zoomOutX( int i ) {
    zoomX += i;
    if( zoomX > Field::WIDTH * 2 ) {
        zoomX = Field::WIDTH * 2;
    }
}

void QtThreadGraphics::zoomOutY( int i ) {
    zoomY += i;
    if( zoomY > Field::HEIGHT * 2 ) {
        zoomY = Field::HEIGHT * 2;
    }
}

void QtThreadGraphics::moveZoomCenterUp( int i ) {
    zoomCenterY -= i;
    if( zoomCenterY < 0 ) {
        zoomCenterY = 0;
    }
}

void QtThreadGraphics::moveZoomCenterDown( int i ) {
    zoomCenterY += i;
    if( zoomCenterY >= Field::HEIGHT ) {
        zoomCenterY = Field::HEIGHT - 1;
    }
}

void QtThreadGraphics::moveZoomCenterRight( int i ) {
    zoomCenterX += i;
    if( zoomCenterX >= Field::WIDTH ) {
        zoomCenterX = Field::WIDTH - 1;
    }
}

void QtThreadGraphics::moveZoomCenterLeft( int i ) {
    zoomCenterX -= i;
    if( zoomCenterX < 0 ) {
        zoomCenterX = 0;
    }
}

void QtThreadGraphics::saveScreen() {
    QImageWriter file( "Screenshot.jpeg" );
    QImage image = *(thread->getImage());
    QPixmap map;

    map = map.fromImage( image );
    map = map.scaled( RenderThread::IMAGEWIDTH, RenderThread::IMAGEHEIGHT );

    file.setQuality( 100 );
    image = map.toImage();

    if( !file.canWrite() ) return;
    file.write( image );
}
